package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BusinessMonth;
import cn.stylefeng.guns.sys.modular.system.mapper.BusinessMonthMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BusinessMonthParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BusinessMonthResult;
import cn.stylefeng.guns.sys.modular.system.service.BusinessMonthService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-06-03
 */
@Service
public class BusinessMonthServiceImpl extends ServiceImpl<BusinessMonthMapper, BusinessMonth> implements BusinessMonthService {
    private Logger logger = LoggerFactory.getLogger(BusinessMonthServiceImpl.class);

    @Override
    public void add(BusinessMonthParam param) {
        BusinessMonth entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BusinessMonthParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(BusinessMonthParam param) {
        BusinessMonth oldEntity = getOldEntity(param);
        BusinessMonth newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BusinessMonthResult findBySpec(BusinessMonthParam param) {
        return null;
    }

    @Override
    public List<BusinessMonthResult> findListBySpec(BusinessMonthParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BusinessMonthParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertData(String month, List<Map<String, Object>> dataList) {

        if (CollectionUtil.isNotEmpty(dataList)) {
            for (Map<String, Object> data : dataList) {
                BusinessMonth entity = new BusinessMonth();
                entity.setShopId(data.get("id").toString());
                entity.setShopCode(data.get("shopCode").toString());
                entity.setShopName(data.get("shopName").toString());
                entity.setPeopleQty(Integer.parseInt(data.get("peopleQty").toString()));
                String monthStr = month.replace("-", "");
                String id = monthStr + "" + entity.getShopId();
                entity.setId(Long.parseLong(id));
                entity.setMonthDate(month);
                try {
                    //先使用此方法，同一店每一月应该只有一条统计数据
                    this.saveOrUpdate(entity);
                } catch (Exception e) {
                    logger.error("定时任务失败，录入就餐人数数据失败-统计{}月就餐人数失败", month);

                    e.printStackTrace();
                }
            }
        }

    }

    private Serializable getKey(BusinessMonthParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BusinessMonth getOldEntity(BusinessMonthParam param) {
        return this.getById(getKey(param));
    }

    private BusinessMonth getEntity(BusinessMonthParam param) {
        BusinessMonth entity = new BusinessMonth();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
